/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.blankie.gui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import org.blankie.model.fs.BlkFSDriver;
import org.blankie.model.fs.BlkFSResource;
import org.blankie.model.fs.BlkFolder;
import org.blankie.util.Organized;

/**
 *
 * @author fmo
 */
public class BlkFSTreeModel implements TreeModel {
    
    static class Node {
        
        Object object;
        
        Node(Object object) {
            this.object = object;
        }
        
        @Override
        public String toString() {
            if (object instanceof List) {
                return "Poste de travail";
            } else {
                return ((BlkFSResource) object).getName();
            }
        }
        
    }

    private EventListenerList listenerList = new EventListenerList();

    private List<BlkFolder> root;
    
    private BlkFSDriver driver;
    
    public BlkFSDriver getDriver() {
        if (driver == null) {
            driver = new BlkFSDriver();
            driver.start();
        }
        
        return driver;
    }

    @Override
    public Object getRoot() {
        if (root == null) {
            root = new ArrayList<BlkFolder>(getDriver().getRoots());
        }
        
        return new Node(root);
    }

    @Override
    public Object getChild(Object parent, int index) {
        parent = ((Node) parent).object;
        if (parent == root) {
            return new Node(root.get(index));
        } else {
            List<BlkFSResource> children = new ArrayList<BlkFSResource>(getDriver().getNodes((Organized) parent));

            if (children.size() <= index) {
                children.addAll(getDriver().getLeaves((Organized) parent));
            }

            return new Node(children.get(index));
        }
    }

    @Override
    public int getChildCount(Object parent) {
        parent = ((Node) parent).object;
        if (parent == root) {
            return root.size();
        } else {
            return getDriver().getNodes((Organized) parent).size() + getDriver().getLeaves((Organized) parent).size();
        }
    }

    @Override
    public boolean isLeaf(Object node) {
        node = ((Node) node).object;
        return node != root && getDriver().isLeaf((Organized) node);
    }

    @Override
    public void valueForPathChanged(TreePath path, Object newValue) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        parent = ((Node) parent).object;
        child = ((Node) child).object;
        
        if (parent == root) {
            return root.indexOf(child);
        } else {
            List<BlkFSResource> children = new ArrayList<BlkFSResource>();

            children.addAll(getDriver().getNodes((Organized) parent));
            children.addAll(getDriver().getLeaves((Organized) parent));

            return children.indexOf(child);
        }
    }

    @Override
    public void addTreeModelListener(TreeModelListener l) {
        listenerList.add(TreeModelListener.class, l);
    }

    @Override
    public void removeTreeModelListener(TreeModelListener l) {
        listenerList.remove(TreeModelListener.class, l);
    }

}
